module.exports = function ($scope, $resource, $modal, toaster) {

	//机构编号查询其游团列表,tag为true查数据不刷新圆圈
	$scope.search = function(pageNo, tag) {
		var para = {
			'pageNo': pageNo,
			'pageSize': $scope.itemsPerPage,
			'company_code': $scope.searchform.code,
			'detail': '1'
		}
		$scope.searchform.loading = tag;
		$scope.groupList = null;
		$resource('/api/ac/atc/groupService/getGroupList', {}, {}).save(para, function(res){
            if (res.errcode === 0) {
				$scope.groupList = res.data.results;
				$scope.totalItems = res.data.totalRecord;
				$scope.currentPage = pageNo;
				$scope.searchform.loading = $scope.groupList.length == 0 ? true : false;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	};

	//加载参加招徕奖励活动的机构名单
	$scope.loadCompany = function() {
		$resource('/api/as/atc/tourist/companySimList', {}, {}).save({}, function(res){
            if (res.errcode === 0) {
				$scope.searchform.company_list = res.data;
				$scope.searchform.company_list.unshift({'company_name': '全部', 'company_code': undefined});
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	}

	init();
	//初始化几页数据并加载补贴规则
    function init() {
		$scope.currentPage = 1;			//当前页码
		$scope.itemsPerPage = 20;		//每页显示几条
		$scope.searchform = {
			'date': {'lable': date2str2(new Date()), 'opened': false}
		};
		$scope.loadCompany();
		$scope.search(1, true);
	};

	//查询游客名单
	$scope.personInfo = function(info) {
		var modalInstance = $modal.open({
			template: require('../views/groupTouristInfo.html'),
			controller: 'groupTouristInfo',
			size: 'lg',
			resolve: {
				info: function () {
					return info;
				}
			}
		});
	}

	//打开日历空间,选择时间
	$scope.dateOpen = function ($event, item) {
		$event.preventDefault();
		$event.stopPropagation();
		item.opened = true;
	};

	//日期转字符串
    function date2str2(d) {
        if (d === undefined) {
            return "";
        }
        if(typeof d === 'string'){
            return d;
        }
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        if (month.length == 1) month = '0' + month;
        if (day.length == 1) day = '0' + day;
        return d.getFullYear() + "-" + month + "-" + day;
    }

};